Skip to content

fix(orama): don't throw on very long tokens in replaceDiacritics - #1055

Open
OsamaAnsar wants to merge 1 commit into
oramasearch:mainfrom
OsamaAnsar:fix/replace-diacritics-long-tokens
Open

OsamaAnsar wants to merge 1 commit into
oramasearch:mainfrom
OsamaAnsar:fix/replace-diacritics-long-tokens

Conversation

@OsamaAnsar

Copy link
Copy Markdown

What was wrong

replaceDiacritics (packages/orama/src/components/tokenizer/diacritics.ts) collects the char codes of the whole token in an array and then does:

return String.fromCharCode(...stringCharCode)

Spreading a very large array into function arguments exceeds the engine's argument/stack limit, so the call throws RangeError: Maximum call stack size exceeded. The tokenizer calls it for every token, so inserting a document with one very long token (a long unbroken blob of text such as base64 or minified content) throws on insert.

Reproduction (script run against main at b030e1b, then against this branch)

                                     before                                              after
replaceDiacritics('a' x 50000)       ok (length 50000)                                   ok (length 50000)
replaceDiacritics('a' x 150000)      RangeError: Maximum call stack size exceeded        ok (length 150000)
replaceDiacritics('a' x 500000)      RangeError: Maximum call stack size exceeded        ok (length 500000)
insert({ text: 'x' x 200000 })       RangeError: Maximum call stack size exceeded        ok

(Node v24.19.0; the exact threshold depends on the engine and stack size.)

Fix

Convert the string in chunks of 8192 characters and concatenate the results. The mapping itself (replaceChar) is unchanged, and the output for short strings is identical.

Tests

Added to packages/orama/tests/diacritics.test.ts: a 500000-character string with diacritics (pattern length 3, so it does not line up with the chunk size) is fully converted; lengths 0, 1, 8191, 8192, 8193, 16384 and 16385; a 150000-character string without diacritics is returned unchanged; and a document with a 200000-character token can be inserted and found.

Verification I ran (Node v24.19.0, tap on the TypeScript sources):

  • Reverted only the source change in diacritics.ts (confirmed with grep -c REPLACE_CHUNK_SIZE going from 3 to 0) and kept the new tests: the three long-string tests fail (3 of 13 assertions; the insert test throws before it can assert). Restored the fix: 14 of 14 pass in that file.
  • Full packages/orama suite (tap tests/*.test.ts) with the fix: 1492 total, 1489 pass, 3 skipped (pre-existing skips), 0 fail. On main the same command gave 1481 total, 1478 pass, 3 skipped. This needed @orama/stopwords and @orama/stemmers built locally first.
  • tsc --noEmit -p packages/orama/tsconfig.json: clean. Formatting checked with Biome 1.9.4 against the repo biome.json: clean. The package lint script is exit 0, so there is no linter to run. I did not run test:types (tsd) or the browser/smoke tests.

Related spread sites I did not change

I left these alone to keep this PR to the one failing path, and did not test them with large inputs: Math.max(...distances) in components/index.ts (geosearch), results.push(...updateResults) / results.push(...insertResults) in methods/upsert.ts, and Math.max.apply(Math, ...) over the result scores in methods/search-hybrid.ts. They could hit the same limit with very large arrays. utils.ts already has a chunked push helper (MAX_ARGUMENT_FOR_STACK) if you want to follow that pattern.

I did not add a changeset or CHANGELOG entry since recent fix PRs do not; happy to add one if you prefer. I am also happy to move the discussion to an issue if that is how you would like to handle it.

replaceDiacritics passed every character code of the token to
String.fromCharCode(...codes), which throws "RangeError: Maximum call
stack size exceeded" once the token is long enough (it worked at 50000
characters and threw at 150000). Indexing a document with such a token,
for example a long base64 blob, failed with that error.

Convert the string in chunks instead.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant